<template>
	<!-- 注册页面的整体盒子 -->
	<div class="reg-container">
		<!-- 注册的盒子 -->
		<div class="reg-box">
			<!-- 注册的标题 -->
			<div class="title-box"></div>
			<!-- 注册的表单区 -->
			<el-form ref="regRef" :model="regForm" :rules="regRules">
				<el-form-item prop="username">
					<el-input placeholder="请输入用户名" v-model="regForm.username"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input placeholder="请输入密码" v-model="regForm.password" type="password"></el-input>
				</el-form-item>
				<el-form-item prop="repassword">
					<el-input placeholder="请再次确认密码" v-model="regForm.repassword" type="password"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="btn-reg" @click="registerBtn">注册</el-button>
					<el-link type="info" @click="$router.push('/login')">去登录</el-link>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>
<script>
import { registerAPI } from '@/api'
export default {
  name: 'my-register',
  data () {
    const samePassword = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        /* 验证失败，调用回调函数，指定一个Error对象 */
        callback(new Error('两次输入的密码不一致'))
      } else {
        /* 验证成功，直接调用callback回调函数即可 */
        callback()
      }
    }
    return {
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      regRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的大小写字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          { validator: samePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /* 注册新用户 */
    registerBtn () {
      /* 进行表单验证 */
      this.$refs.regRef.validate(async valid => {
        if (!valid) return false
        /* 调用注册接口 */
        const { data: res } = await registerAPI(this.regForm)
        console.log(res.code)
        /* 注册失败，提示用户 */
        if (res.code !== 0) return this.$message.error(res.message)
        /* 注册成功 提示用户 */
        if (res.code === 0) {
          this.$message.success(res.message)
          /* 跳转到登录页面 */
          this.$router.push('/login')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.reg-container {
	background: url('../../assets/images/login_bg.jpg') center;
	height: 100%;
	background-size: cover;

	.reg-box {
		width: 400px;
		height: 335px;
		background-color: #fff;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		padding: 0 30px;
		background-color: #fff;

		.title-box {
			background: url('../../assets/images/login_title.png') center no-repeat;
			height: 60px;
		}

		.btn-reg {
			width: 100%;
		}
	}
}
</style>
